<?php
require("../includes/classes/player_class.php");
require("../includes/classes/item_class.php");
require_once("../includes/db_connect.php");

$errmsg_arr = array();
$errflag = false;

$xmlString = $_POST['raidxml'];
@$xmlDoc = simplexml_load_string($xmlString);

if($xmlDoc == NULL)
{
    $errmsg_arr[] = "Not valid XML string";
    $errflag = true;
}

if($errflag)
{
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: import.php");
    exit();
}

// Checks if there are players in the string
if(count($xmlDoc->PlayerInfos->children()) < 1)
{
    $errmsg_arr[] = "No players found in string";
    $errflag = true;
}

// Checks if there are loots in the string
if(count($xmlDoc->Loot->children()) < 1)
{
    $errmsg_arr[] = "No loot found in string";
    $errflag = true;
}

if($errflag)
{
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: import.php");
    exit();
}

// Store each player into a Player array
$players = array();
$attendance = array();

foreach($xmlDoc->PlayerInfos->children() as $child)
{
    if($child->name != "")
    {
        $player = new Player(clean($child->name), clean($child->race), clean($child->class));
        $players[] = $player;
        $attendance[] = $child->name;
    }
}

// Parses items
$items = array();

foreach($xmlDoc->Loot->children() as $child)
{
    if($child->ItemName != "")
    {
        $item = new Item($child->ItemName, $child->Player, $child->ItemID);
        $items[] = $item;
    }
}

// If actual boss info exists, then we'll use the attendance list there instead
if(count($xmlDoc->BossKills->children()) > 0)
{
    $attendance = NULL;
    $attendance = array();

    $bossinfo = $xmlDoc->BossKills->key1;
    $bossname = $bossinfo->name;

    foreach($bossinfo->attendees->children() as $child)
    {
        $attendance[] = $child->name;
    }
}
?>